#define MM_RTV_INCLUDED 1


#if !defined MM_GLOBALS_INCLUDED
#include "include/mm_globals.inl"
#endif

new gCvarRtv;
new gCvarRtvAdmin;
new gCvarRtvDelay;
new gCvatRtvPercent;

new gPlayersRtv[MAX_PLAYERS];
new gRtvCount;
new rtvReached;

public plugin_init_rtv(){
	gCvarRtv = register_cvar("mm_rtv", "1");
	gCvarRtvAdmin = register_cvar("mm_rtv_admin", "1");
	gCvarRtvDelay = register_cvar("mm_rtv_delay", "300");
	gCvatRtvPercent = register_cvar("mm_rtv_percent", "70");
	register_clcmd("say rtv", "saidRtv");
	register_clcmd("say rockthevote", "saidRtv");
	register_clcmd("amx_rtv", "adminRtv", ADMIN_BAN);
}

stock client_connect_rtv(id){
	if(!rtvReached){
		gPlayersRtv[id] = 0;
	}
}
stock client_disconnect_rtv(id){
	if(!rtvReached){
		new minTime = get_pcvar_num(gCvarRtvDelay);
		new timeNow = floatround(get_gametime());
		if(minTime > timeNow){
			if(gPlayersRtv[id] ){
				gRtvCount--;
				gPlayersRtv[id] = 0;
				checkRtv();
			}
		}
	}
}

public adminRtv(id,level,cid){
	if(!get_pcvar_num(gCvarActive))
		return PLUGIN_CONTINUE;
	if(!get_pcvar_num(gCvarRtvAdmin)){
		client_print(id, print_chat, "%L", LANG_PLAYER, "OPTION_OFF");
		return PLUGIN_HANDLED;
	}
	client_print(0, print_chat, "%L %L", LANG_PLAYER, "ADMIN_USED_RTV", LANG_PLAYER, "VOTE_IN_NEXT_ROUND"); 
	set_pcvar_float(gCvarMP_TIMELIMIT, (get_gametime()  + getSecondsToRoundEnd() + 20.0) / 60.0);
	rtvReached =1;
	return PLUGIN_HANDLED;
}

public saidRtv(id){
	if(!get_pcvar_num(gCvarActive))
		return PLUGIN_CONTINUE;
	if(rtvReached)
		return PLUGIN_CONTINUE;
	if(gMapVoteStarted){
		client_print(id, print_chat, "%L", LANG_PLAYER, "NO_RTV_WHILE_VOTING");
		return PLUGIN_CONTINUE;
	}
	if(get_pcvar_num(gCvarRtv)){
		
		if(gPlayersRtv[id]){
			client_print(id, print_chat, "%L", LANG_PLAYER, "RTV_ONCE");
			checkRtv();
		}
		else {
			new minTime = get_pcvar_num(gCvarRtvDelay);
			new timeNow = floatround(get_gametime());
			if(minTime > timeNow){
				new timeleft = minTime - timeNow;
				new m = timeleft / 60;
				new s = timeleft % 60;
				client_print(id, print_chat, "%L",LANG_PLAYER, "RTV_NOT_IN_TIME", m,(s<10?"0":""),s);
			} else {
				gPlayersRtv[id] = 1;
				gRtvCount++;
				new name[MAX_NAME_LEN];
				get_user_name(id, name , MAX_NAME_LEN -1);
				client_print(0, print_chat, "%L", LANG_PLAYER, "PLAYER_USED_RTV", name);
				checkRtv();
			}
		}
	} else {
		client_print(id, print_chat, "%L", LANG_PLAYER, "OPTION_OFF");
	}
	return PLUGIN_CONTINUE;
}

stock checkRtv(){
	if(!get_pcvar_num(gCvarActive))
		return;
	if(!get_pcvar_num(gCvarRtv))
		return;
	if(gMapVoteStarted)
		return;
	if(rtvReached)
		return;
		
	new num = getRealPlayersnum();
	new percent = get_pcvar_num(gCvatRtvPercent);
	new minNum = floatround(float(num) / 100.0 * float(percent), floatround_ceil);

	if(gRtvCount >= minNum){
		client_print(0, print_chat, "%L %L", LANG_PLAYER, "RTV_ENOUGH", LANG_PLAYER, "VOTE_IN_NEXT_ROUND");
		set_pcvar_float(gCvarMP_TIMELIMIT, (get_gametime()  + getSecondsToRoundEnd() + 20.0) / 60.0);
		rtvReached = 1;
	} else {
		client_print(0, print_chat, "%L", LANG_PLAYER, "MORE_NEEDED",  minNum - gRtvCount);
	}
}
